package nets

import (
	"bytes"
	"cli/config"
	"encoding/json"
	"fmt"
	"io"
	"net/http"

	"github.com/k0kubun/pp"
)

func Resume(classId, userId int) {
	targetUrl := config.Config.CliInfo.Url + "/api/root/resume/by/userId/classId"
	dataMap := make(map[string]interface{})
	dataMap["user_id"] = userId
	dataMap["class_id"] = classId
	dataMap["sudoAuthentication"] = AuthenticationInfo
	payload, _ := json.Marshal(dataMap)
	req, _ := http.NewRequest("POST", targetUrl, bytes.NewReader(payload))
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	resqData, _ := io.ReadAll(response.Body)
	if response.StatusCode != 200 {
		fmt.Println("Error:", string(resqData))
	} else {
		fmt.Println(string(resqData))
	}
}
func CancelResume(classId, userId int) {
	targetUrl := config.Config.CliInfo.Url + "/api/root/cancel/resume"
	dataMap := make(map[string]interface{})
	dataMap["user_id"] = userId
	dataMap["class_id"] = classId
	dataMap["sudoAuthentication"] = AuthenticationInfo
	payload, _ := json.Marshal(dataMap)
	req, _ := http.NewRequest("POST", targetUrl, bytes.NewReader(payload))
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	resqData, _ := io.ReadAll(response.Body)
	if response.StatusCode != 200 {
		fmt.Println("Error:", string(resqData))
	} else {
		fmt.Println(string(resqData))
	}
}
func SelectActivedClass() {
	targetUrl := config.Config.CliInfo.Url + "/api/select/actived/class"
	dataMap := make(map[string]interface{})
	dataMap["sudoAuthentication"] = AuthenticationInfo
	payload, _ := json.Marshal(dataMap)
	req, _ := http.NewRequest("POST", targetUrl, bytes.NewReader(payload))
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	resqData, _ := io.ReadAll(response.Body)
	if response.StatusCode != 200 {
		fmt.Println("Error:", string(resqData))
	}
	var data [][]*Class
	err = json.Unmarshal(resqData, &data)
	if err != nil {
		fmt.Println("Error unmarshaling JSON:", err)
		return
	}

	// 打印解析后的数据
	for i, classes := range data {
		fmt.Println("--------------------------------------")
		fmt.Printf("第 %d组:\n", i)
		for index, class := range classes {
			if class != nil {
				fmt.Println("第", index+1, "个班级的班级id:", class.ClassId)
				fmt.Println("该班级的具体内容为")
				pp.Print(class.ClassActived)
			}
		}
	}
	fmt.Println("--------------------------------------")
}
func SelectClassTabel() {
	targetUrl := config.Config.CliInfo.Url + "/api/select/classTabel"
	req, _ := http.NewRequest("POST", targetUrl, nil)
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	resqData, _ := io.ReadAll(response.Body)
	if response.StatusCode != 200 {
		fmt.Println("Error:", string(resqData))
	}
	var classTabel ClassTabel
	json.Unmarshal(resqData, &classTabel)
	pp.Print(classTabel)
}
